﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Niti.OrderManage.OrderManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace Niti.OrderManage
{
    [ConnectionStringName("MySQL")]
    public class NitiOrderManageDbContext : AbpDbContext<NitiOrderManageDbContext>
    {
        public NitiOrderManageDbContext(DbContextOptions<NitiOrderManageDbContext> options) : base(options)
        {
        }

        #region DbSet
        public DbSet<Supplier> Suppliers { get; set; }
        public DbSet<Commodity> Commoditys { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderLineItem> OrderLineItems { get; set; }
        public DbSet<ReceiptOrder> ReceiptOrders { get; set; }
        public DbSet<ReceiptOrderLineItem> ReceiptOrderLineItems { get; set; }
        public DbSet<CheckOutOrder> CheckOutOrders { get; set; }
        public DbSet<CheckOutOrderLineItem> CheckOutOrderLineItems { get; set; }

        #endregion

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Configure();
        }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            //输出sql语句到vs窗口
            optionsBuilder.LogTo((msg) => System.Diagnostics.Trace.WriteLine(msg), new[] { Microsoft.EntityFrameworkCore.Diagnostics.RelationalEventId.CommandExecuting });
            optionsBuilder.EnableSensitiveDataLogging();

        }

        //public override int SaveChanges()
        //{
        //    //return base.SaveChanges();
        //    //从DomainEvents中拿到定义的领域事件
        //    var changeReport = ApplyAbpConcepts();
        //    var result = base.SaveChanges();
        //    //数据保存后，触发领域事件
        //    EntityChangeEventHelper.TriggerEvents(changeReport);
        //    return result;
        //}



        public static readonly ILoggerFactory DebugLoggerFactory = LoggerFactory.Create(builder =>
        {
            builder.AddFilter((category, level) =>
            category == DbLoggerCategory.Database.Command.Name
            && level == LogLevel.Information).AddDebug();
        });
    }
}
